package com.sunricher.telinkblemeshlib.db;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import com.sunricher.telinkblemeshlib.models.UartDaliDevice;

import java.util.ArrayList;
import java.util.List;

@Database(entities = {UartDaliDevice.class}, version = 1)
abstract class UartDaliDeviceDatabase extends RoomDatabase {

    private static volatile UartDaliDeviceDatabase instance;

    private Context context;

    public void setContext(Context context) {
        this.context = context;
    }

    public Context getContext() {
        return context;
    }

    static UartDaliDeviceDatabase getInstance(Context context) {
        if (instance == null) {
            synchronized (UartDaliDeviceDatabase.class) {
                if (instance == null) {
                    instance = Room.databaseBuilder(context, UartDaliDeviceDatabase.class, "UartDaliDevice").allowMainThreadQueries().build();
                    instance.setContext(context);
                }
            }
        }
        return instance;
    }

    abstract UartDaliDeviceDao uartDaliDeviceDao();

    void insert(UartDaliDevice device) {
        uartDaliDeviceDao().insert(device);
    }

    UartDaliDevice getDevice(int daliAddress, int gatewayAddress) {
        return uartDaliDeviceDao().select(daliAddress, gatewayAddress);
    }

    List<UartDaliDevice> getDevices(int gatewayAddress) {
        return uartDaliDeviceDao().selectAll(gatewayAddress);
    }

    void deleteAll(int gatewayAddress) {
        uartDaliDeviceDao().deleteAll(gatewayAddress);
    }

    void delete(int daliAddress, int gatewayAddress) {
        uartDaliDeviceDao().delete(daliAddress, gatewayAddress);
    }

}
